package com.cn.sys.frame.tag;

import java.util.List;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cn.sys.common.Constants;
import com.cn.sys.main.entity.SysFunction;

public class PrivilegeTag extends TagSupport {

	private static final long serialVersionUID = 2281114143086049741L;
	
	private static Logger logger = LoggerFactory.getLogger(PrivilegeTag.class);
	private String code; 
	
	@SuppressWarnings("unchecked")
	@Override
	public int doStartTag() throws JspException {
		HttpSession session = this.pageContext.getSession();
		int result = 0;
		try {
			List<SysFunction> list = (List<SysFunction>) session.getAttribute(Constants.USER_FUNCTIONS);
			if(list!=null){
				for (SysFunction func : list) {
					if(code!=null&&code.equals(func.getFunctionCode())){
						result = 1;
						break;
					}
				}
			}
		} catch (Exception e) {
			//e.printStackTrace();
			logger.info("权限控制出错! ["+e.getMessage()+"]");
			result = 0;
		}
		return result;
	}
	
	@Override
	public int doEndTag() throws JspException {
		return super.doEndTag();
	}

	@Override
	public void release() {
		super.release();
		this.code = null;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	
}
